﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DgvPage
{
    public partial class ControlDgvPage : UserControl
    {
        #region 参数
        //定义分页需要的变量
        int intTotal = 0;
        int intPages = 0;
        int intPageCurrent = 0;
        int intPageSize = 10;
        Type type = null;
        private Object dataSorce = null;
        /// <summary>
        /// 是否显示鼠标进入闪动效果
        /// </summary>
        /// <value><c>true</c> if this instance is show tips; otherwise, <c>false</c>.</value>
        [Description("显示datatgridview"), Category("自定义")]
        public DataGridView DisplayDgv { get; set; } = null;

        #endregion

        public ControlDgvPage()
        {
            InitializeComponent();
            this.Load += ControlDgvPage_Load;
        }

        private void ControlDgvPage_Load(object sender, EventArgs e)
        {
            InitializeValue();
            InitializeEvent();
        }

        private void InitializeEvent()
        {
            this.cbb_PageDataCount.SelectedIndexChanged += Cbb_PageDataCount_SelectedIndexChanged;
            btn_First.Click += Btn_Click;
            btn_Last.Click += Btn_Click;
            btn_Left.Click += Btn_Click;
            btn_Right.Click += Btn_Click;
            txt_CurrentPageNo.TextChanged += Txt_CurrentPageNo_TextChanged;
        }



        private void InitializeValue()
        {
            this.cbb_PageDataCount.SelectedIndex = 0;
        }

        #region 方法

        /// <summary>
        /// 设置数据源
        /// </summary>
        /// <param name="list">List<object> </param>
        public void SetDataSource(Object Data, bool isChangeCurrent = true)
        {
            if (DisplayDgv == null)
            {
                this.Enabled = false;
                return;
            }
            DisplayDgv.AutoGenerateColumns = false;
            if (Data == null)
            {
                DisplayDgv.DataSource = null;
                dataSorce = null;
                this.Enabled = false;
                intTotal = 0;
                intPages = 0;
                intPageCurrent = 1;
                lbl_DataInfo.Text = "";
                txt_CurrentPageNo.Text = "0";
                return;
            }
            type = Data.GetType();
            if (type == typeof(List<object>))
            {
                List<object> list = (List<object>)Data;
                intTotal = list.Count;
            }
            if (type == typeof(DataTable))
            {
                DataTable table = (DataTable)Data;
                intTotal = table.Rows.Count;
            }
            if (intTotal == 0)
            {
                dataSorce = null;
                this.Enabled = false;
                intTotal = 0;
                intPages = 0;
                intPageCurrent = 1;
                lbl_DataInfo.Text = "";
                txt_CurrentPageNo.Text = "0";
                return;
            }
            dataSorce = Data;
            this.Enabled = true;
            intPages = intTotal / intPageSize + ((intTotal % intPageSize) > 0 ? 1 : 0);
            if (isChangeCurrent || intPageCurrent == 0)
            {
                intPageCurrent = 1;
                txt_CurrentPageNo.Text = intPageCurrent.ToString();
            }

            LoadData();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            try
            {
                if (DisplayDgv == null)
                {
                    this.Enabled = false;
                    return;
                }
                if (dataSorce == null)
                {
                    DisplayDgv.DataSource = null;
                    dataSorce = null;
                    this.Enabled = false;
                    intTotal = 0;
                    intPages = 0;
                    intPageCurrent = 1;
                    lbl_DataInfo.Text = "";
                    txt_CurrentPageNo.Text = "0";
                    return;
                }
                if (intPageCurrent == 1)
                {
                    btn_Left.Enabled = btn_First.Enabled = false;
                }
                else
                {
                    btn_Left.Enabled = btn_First.Enabled = true;
                }
                if (intPages == intPageCurrent)
                {
                    btn_Right.Enabled = btn_Last.Enabled = false;
                }
                else
                {
                    btn_Right.Enabled = btn_Last.Enabled = true;
                }
                if (intPageCurrent > intPages)
                {
                    intPageCurrent = intPages;
                    txt_CurrentPageNo.Text = intPageCurrent.ToString();
                    return;
                }

                int startNo = (intPageCurrent - 1) * intPageSize;
                int endNo = intPageCurrent == intPages ? intTotal : intPageCurrent * intPageSize;
                lbl_DataInfo.Text = string.Format("显示 {0} 条，共计 {1} 页，总计 {2} 条", (startNo + 1).ToString() + "--" + endNo.ToString(), intPages.ToString(), intTotal.ToString());
                if (type == typeof(List<object>))
                {
                    List<object> list = (List<object>)dataSorce;
                    List<object> listTemp = new List<object>();
                    if (list.Count != 0)
                    {
                        for (int i = startNo; i < endNo; i++)
                        {
                            listTemp.Add(list[i]);
                        }
                        DisplayDgv.DataSource = listTemp;


                    }
                }
                if (type == typeof(DataTable))
                {
                    DataTable table = (DataTable)dataSorce;
                    DataTable dtTemp = table.Clone();
                    if (table.Rows.Count != 0)
                    {
                        for (int i = startNo; i < endNo; i++)
                        {
                            dtTemp.ImportRow(table.Rows[i]);
                        }

                        DisplayDgv.DataSource = dtTemp;


                    }

                }
            }
            catch (Exception ex)
            {

            }
        }

        #endregion

        #region 事件
        private void Btn_Click(object sender, EventArgs e)
        {
            if (DisplayDgv == null)
            {
                this.Enabled = false;
                return;
            }
            if (dataSorce == null)
            {
                DisplayDgv.DataSource = null;
                dataSorce = null;
                this.Enabled = false;
                intTotal = 0;
                intPages = 0;
                intPageCurrent = 1;
                lbl_DataInfo.Text = "";
                txt_CurrentPageNo.Text = "0";
                return;
            }
            Button btn = (Button)sender;
            switch (btn.Name)
            {
                case "btn_First":
                    intPageCurrent = 1;
                    btn_First.Enabled = btn_Left.Enabled = false;
                    btn_Last.Enabled = btn_Right.Enabled = true;
                    //LoadData();
                    break;
                case "btn_Last":
                    intPageCurrent = intPages;
                    btn_First.Enabled = btn_Left.Enabled = true;
                    btn_Last.Enabled = btn_Right.Enabled = false;
                    //LoadData();
                    break;
                case "btn_Left":
                    btn_Last.Enabled = btn_Right.Enabled = true;
                    if (intPageCurrent == 1)
                    {
                        btn_First.Enabled = btn_Left.Enabled = false;
                        MessageBox.Show("已经是第一页了");
                        return;
                    }
                    intPageCurrent--;
                    //LoadData();
                    break;
                case "btn_Right":
                    btn_Last.Enabled = btn_Right.Enabled = true;
                    if (intPageCurrent == intPages)
                    {
                        btn_Last.Enabled = btn_Right.Enabled = false;
                        MessageBox.Show("已经是最后一页了");
                        return;
                    }
                    intPageCurrent++;
                    //LoadData();
                    break;

            }
            txt_CurrentPageNo.Text = intPageCurrent.ToString();
        }

        private void Cbb_PageDataCount_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DisplayDgv == null)
            {
                this.Enabled = false;
                return;
            }
            if (dataSorce == null)
            {
                DisplayDgv.DataSource = null;
                dataSorce = null;
                this.Enabled = false;
                intTotal = 0;
                intPages = 0;
                intPageCurrent = 1;
                lbl_DataInfo.Text = "";
                txt_CurrentPageNo.Text = "0";
                return;
            }
            Int32.TryParse(cbb_PageDataCount.Text.Trim(), out intPageSize);
            if (intPageSize == 0)
            { intPageSize = 10; }
            intPages = intTotal / intPageSize + ((intTotal % intPageSize) > 0 ? 1 : 0);
            if (intPageCurrent > intPages)
            {
                intPageCurrent = intPages;
                txt_CurrentPageNo.Text = intPageCurrent.ToString();
            }
            else
            {
                LoadData();
            }

        }

        private void Txt_CurrentPageNo_TextChanged(object sender, EventArgs e)
        {
            if (DisplayDgv == null)
            {
                this.Enabled = false;
                return;
            }
            if (dataSorce == null)
            {
                DisplayDgv.DataSource = null;
                dataSorce = null;
                this.Enabled = false;
                intTotal = 0;
                intPages = 0;
                intPageCurrent = 1;
                lbl_DataInfo.Text = "";
                txt_CurrentPageNo.Text = "0";
                return;
            }
            Int32.TryParse(txt_CurrentPageNo.Text.Trim(), out intPageCurrent);
            if (intPageCurrent == 0)
            {
                intPageCurrent = 1;
                txt_CurrentPageNo.Text = intPageCurrent.ToString();
            }
            if (intPageCurrent > intPages)
            {
                intPageCurrent = intPages;
                txt_CurrentPageNo.Text = intPageCurrent.ToString();
            }
            LoadData();
        }
        #endregion
    }
}
